package sf.querydsl.mapper;

import com.querydsl.sql.AbstractSQLQuery;
import com.querydsl.sql.SQLQuery;
import sf.common.wrapper.Page;
import sf.core.DBObject;
import sf.database.dao.DBClient;
import sf.database.dao.DBContext;
import sf.querydsl.QueryDSLTables;
import sf.querydsl.SQLRelationalPath;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * @param <T>
 */
public class QueryDSLMapperImpl<T extends DBObject> implements QueryDSLMapper<T> {
    protected DBClient client;
    protected Class<T> domainClass;

    public QueryDSLMapperImpl(DBClient client, Class<T> domainClass) {
        this.client = client;
        this.domainClass = domainClass;
    }

    @Override
    public SQLRelationalPath<T> queryDSLTable() {
        return QueryDSLTables.relationalPathBase(null, domainClass);
    }

    @Override
    public SQLRelationalPath<T> queryDSLTable(DBContext context) {
        return QueryDSLTables.relationalPathBase(context, domainClass);
    }

    @Override
    public List<T> queryDSLSelectList(AbstractSQLQuery<T, SQLQuery<T>> query) {
        return client.getQueryDSL().queryDSLSelectList(query, domainClass);
    }

    @Override
    public Page<T> queryDSLSelectPage(AbstractSQLQuery<T, SQLQuery<T>> query, int start, int limit) {
        return client.getQueryDSL().queryDSLSelectPage(query, domainClass, start, limit);
    }

    @Override
    public <S> S queryDSLSelectOne(AbstractSQLQuery<T, SQLQuery<T>> query, Class<S> clz) {
        return client.getQueryDSL().queryDSLSelectOne(query, clz);
    }

    @Override
    public T queryDSLSelectOne(AbstractSQLQuery<T, SQLQuery<T>> query) {
        return client.getQueryDSL().queryDSLSelectOne(query, domainClass);
    }

    @Override
    public void queryDSLSelectIterator(Consumer<Iterable<T>> ormIt, AbstractSQLQuery<T, SQLQuery<T>> query) {
        client.getQueryDSL().queryDSLSelectIterator(ormIt, domainClass, query);
    }

    @Override
    public void queryDSLSelectStream(Consumer<Stream<T>> ormStream, AbstractSQLQuery<T, SQLQuery<T>> query) {
        client.getQueryDSL().queryDSLSelectStream(ormStream, domainClass, query);
    }
}
